rm(list=ls())
library(tidyverse)
library(ggpubr)
library(haven)

anes <- read_spss("ANES files/anes_timeseries_cdf_spss_20220916.sav")


anes2 <- anes %>%
  select(c(weight_full = VCF0009z, #weights for full sample
           #weight_mode = VCF0009x, # FTF weights DROP 2020
           #weight_web = VCF0009y, # web weights DROP 2020
           weight_post_full = VCF9999, # post-election full weights
           year = VCF0004, # year
           FT_dem = VCF0218 , # Dem Party FT
           FT_rep = VCF0224, # Rep Party FT
           pid = VCF0301, # PID
           sex = VCF0104, #1 Male, 2 Female, 3 Other
           race = VCF0105a,
           mode = VCF0017, #mode of survey, only want 0 (FTF) or 4 (web) later
           extEfcacy = VCF0648,
           satDem = VCF9255
  )) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  sapply(as.numeric) %>% data.frame()


A24 <-  read_spss("ANES files/anes_timeseries_2024_spss_20250430.sav")

anes24 <- A24 %>% #2024 ANES
  mutate(year = 2024) %>%     
  select(weight_post_full =  V240107b,   # weights, Post full ANES sample (FTF + panel + web + PAPI)
         weight_full =   V240107a, # 
         #weight_ftf = V240101a, # FTF weights DROP 2020
         #weight_web = V240102a, # web weights DROP 2020
         FT_dem = V241166,
         FT_rep = V241167,
         pid =  V241227x,
         mode = V240002a, #only want 1 (FTF) or 2 (web)
         year,
         sex = V241550, #1. Male, 2 Female
         race = V241501x,
         satDem = V242439,
         extEfcacy_official_dont_care = V242200, # POST: [STD] PUBLIC OFFICIALS DON’T CARE WHAT PEOPLE THINK, USE IT TO BUILD EFFICACY INDEX. 5-PT SCALE HERE UNLIKE ANES CDF
         extEfcacy_have_no_say = V242201 # POST: [STD] HAVE NO SAY ABOUT WHAT GOVERNMENT DOES, USE IT TO BUILD EFFICACY INDEX. 5-PT SCALE HERE UNLIKE ANES CDF     
  ) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  subset(FT_dem >= 0 & FT_rep >=0)%>%
  sapply(as.numeric) %>% data.frame() #%>%
# mutate(extEfcacy = (extEfcacy_official_dont_care+extEfcacy_have_no_say)/2)

anes3 <- bind_rows(anes2, anes24)

#check
summary(anes3$year) #perf

##########################################################################
#create pid dummy
anes3$pid_dum <- ifelse(anes3$pid < 4, "Democrat", "Republican")

#create inparty and outparty
anes3$inparty <- ifelse(anes3$pid_dum=="Democrat", "Democrat", "Republican")
anes3$outparty <- ifelse(anes3$pid_dum=="Democrat", "Republican", "Democrat")

#link FTs to in and out_party
anes3$inparty_FT <- ifelse(anes3$pid_dum=="Democrat", anes3$FT_dem, anes3$FT_rep)
anes3$outparty_FT <- ifelse(anes3$pid_dum=="Democrat", anes3$FT_rep, anes3$FT_dem)


###caclulate AP as IN_FT - OUT_FT
anes3$AP <- anes3$inparty_FT - anes3$outparty_FT



#################### PARTY
polar <- anes3 %>%
  select(year, AP, pid_dum, weight_full) %>%
  drop_na() %>%
  group_by(year, pid_dum) %>%
  summarise(value = weighted.mean(AP, weight_full),
            sd = sd(AP*weight_full),
            se = sd/sqrt(n()))


a<-ggplot(polar, aes(x=year, y=value, color=pid_dum)) +
  geom_line(linetype="dashed") +
  geom_point() +
  #  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3")) +
  labs(x="", y="Mean",
       title="Affective Polarization", color="Party",
       subtitle="Feeling thermometer ratings of one's own party (0 to 100) minus 
Feeling thermometer ratings of one’s rival party (0 to 100). Only partisans 
(including partisan leaners). N = 43,062 (ANES).
Means and 95% Confidence Intervals.")+
  scale_y_continuous(limits=c(0,100))


###satdem
satis <- anes3 %>%
  select(year, weight_full, satDem, pid_dum) %>%
  drop_na() %>%
  subset(satDem >=0) %>%
  mutate(satDem_reversed = ((satDem - 5)*-1)+1) %>%
  group_by(year, pid_dum)%>%
  summarise(value = weighted.mean(satDem_reversed, weight_full),
            sd = sd(satDem_reversed*weight_full),
            se = sd/sqrt(n()))

b<-ggplot(satis, aes(x=year, y=value, color=pid_dum)) +
  geom_point() +
  geom_line(linetype="dashed")+
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"))+
  scale_x_continuous(breaks=c(2005,2010,2015,2020, 2024))+
  labs(x="", y="Category",
       title="Satisfaction with Democracy", color="Party",
       subtitle="On the whole, are you satisfied with the way
democracy works in the United States? N=21,406 (ANES)
Means and 95% Confidence Intervals") +
  scale_y_continuous(limits=c(2,5),labels = c("Not at all satisfied",
                                              "Not very satisfied",
                                              "Fairly satisfied",
                                              "Very Satisfied"))


eff <- anes3 %>%
  subset((extEfcacy_official_dont_care >=0 & extEfcacy_have_no_say>=0) |
           (is.na(extEfcacy_official_dont_care) & is.na(extEfcacy_have_no_say)))


eff$extEfcacy <- ifelse(eff$year==2024, ((eff$extEfcacy_official_dont_care + eff$extEfcacy_have_no_say)/2)*10, eff$extEfcacy)

eff <- eff %>%
  select(year, weight_full, extEfcacy, pid_dum) %>%
  drop_na() %>%
  group_by(year, pid_dum) %>%
  summarise(value = weighted.mean(extEfcacy, weight_full),
            sd = sd(extEfcacy*weight_full),
            se = sd/sqrt(n()))

c<-ggplot(eff, aes(x=year, y=value, color=pid_dum)) +
  geom_line(linetype="dashed") +
  geom_point() +
  #  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"))+
  scale_y_continuous(limits=c(0,100)) +
  labs(x="", y="Mean",
       title="External Efficacy", color="Party",
       subtitle="Based on two items: People like me don't have any say
about what the government does, Agree (0), Neither (50), Disagree (100) /
I don't think public officials care much what people like me think, 
Agree (0), Neither (50), Disagree (100). N = 59,492 (ANES). 
Means and 95% Confidence Intervals.")

png(file = "PNAS/panel2-by-party.png",
    height = 6,
    width = 18,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(b,a,c,
                nrow = 1, ncol = 3, common.legend = T, legend = "bottom"))  

dev.off()



#################### SEX
polar <- anes3 %>%
  select(year, AP, sex, weight_full) %>%
  subset(sex==1 | sex==2) %>%
  drop_na() %>%
  group_by(year, sex) %>%
  summarise(value = weighted.mean(AP, weight_full),
            sd = sd(AP*weight_full),
            se = sd/sqrt(n()))


a<-ggplot(polar, aes(x=year, y=value, color=as.character(sex))) +
  geom_line(linetype="dashed") +
  geom_point() +
  #  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"), labels =c("Male", "Female")) +
  labs(x="", y="Mean",
       title="Affective Polarization", color="Sex",
       subtitle="Feeling thermometer ratings of one's own party (0 to 100) minus 
Feeling thermometer ratings of one’s rival party (0 to 100). Only partisans 
(including partisan leaners). N = 43,062 (ANES).
Means and 95% Confidence Intervals.")+
  scale_y_continuous(limits=c(0,100))
a

###satdem
satis <- anes3 %>%
  select(year, weight_full, satDem, sex) %>%
  subset(sex==1 | sex==2) %>%
  drop_na() %>%
  subset(satDem >=0) %>%
  mutate(satDem_reversed = ((satDem - 5)*-1)+1) %>%
  group_by(year, sex)%>%
  summarise(value = weighted.mean(satDem_reversed, weight_full),
            sd = sd(satDem_reversed*weight_full),
            se = sd/sqrt(n()))

b<-ggplot(satis, aes(x=year, y=value, color=as.character(sex))) +
  geom_point() +
  geom_line(linetype="dashed")+
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"), labels=c("Male", "Female"))+
  scale_x_continuous(breaks=c(2005,2010,2015,2020, 2024))+
  labs(x="", y="Category",
       title="Satisfaction with Democracy", color="Sex",
       subtitle="On the whole, are you satisfied with the way
democracy works in the United States? N=21,406 (ANES)
Means and 95% Confidence Intervals") +
  scale_y_continuous(limits=c(2,5),labels = c("Not at all satisfied",
                                              "Not very satisfied",
                                              "Fairly satisfied",
                                              "Very Satisfied"))
b

eff <- anes3 %>%
  subset((extEfcacy_official_dont_care >=0 & extEfcacy_have_no_say>=0) |
           (is.na(extEfcacy_official_dont_care) & is.na(extEfcacy_have_no_say)))


eff$extEfcacy <- ifelse(eff$year==2024, ((eff$extEfcacy_official_dont_care + eff$extEfcacy_have_no_say)/2)*10, eff$extEfcacy)

eff <- eff %>%
  select(year, weight_full, extEfcacy, sex) %>%
  subset(sex==1 | sex==2) %>%
  drop_na() %>%
  group_by(year, sex) %>%
  summarise(value = weighted.mean(extEfcacy, weight_full),
            sd = sd(extEfcacy*weight_full),
            se = sd/sqrt(n()))

c<-ggplot(eff, aes(x=year, y=value, color=as.character(sex))) +
  geom_line(linetype="dashed") +
  geom_point() +
  #  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"), labels=c("Male", "Female"))+
  scale_y_continuous(limits=c(0,100)) +
  labs(x="", y="Mean",
       title="External Efficacy", color="Sex",
       subtitle="Based on two items: People like me don't have any say
about what the government does, Agree (0), Neither (50), Disagree (100) /
I don't think public officials care much what people like me think, 
Agree (0), Neither (50), Disagree (100). N = 59,492 (ANES). 
Means and 95% Confidence Intervals.")
c

png(file = "PNAS/panel2-by-sex.png",
    height = 6,
    width = 18,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(b,a,c,
                nrow = 1, ncol = 3, common.legend = T, legend = "bottom"))  

dev.off()



######## RACE
polar <- anes3 %>%
  select(year, AP, race, weight_full) %>%
  subset(race==1 | race==2) %>%
  drop_na() %>%
  group_by(year, race) %>%
  summarise(value = weighted.mean(AP, weight_full),
            sd = sd(AP*weight_full),
            se = sd/sqrt(n()))


a<-ggplot(polar, aes(x=year, y=value, color=as.character(race))) +
  geom_line(linetype="dashed") +
  geom_point() +
  #  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"), labels =c("White", "Black")) +
  labs(x="", y="Mean",
       title="Affective Polarization", color="Race",
       subtitle="Feeling thermometer ratings of one's own party (0 to 100) minus 
Feeling thermometer ratings of one’s rival party (0 to 100). Only partisans 
(including partisan leaners). N = 43,062 (ANES).
Means and 95% Confidence Intervals.")+
  scale_y_continuous(limits=c(0,100))
a

###satdem
satis <- anes3 %>%
  select(year, weight_full, satDem, race) %>%
  subset(race==1 | race==2) %>%
  drop_na() %>%
  subset(satDem >=0) %>%
  mutate(satDem_reversed = ((satDem - 5)*-1)+1) %>%
  group_by(year, race)%>%
  summarise(value = weighted.mean(satDem_reversed, weight_full),
            sd = sd(satDem_reversed*weight_full),
            se = sd/sqrt(n()))

b<-ggplot(satis, aes(x=year, y=value, color=as.character(race))) +
  geom_point() +
  geom_line(linetype="dashed")+
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"), labels=c("White", "Black"))+
  scale_x_continuous(breaks=c(2005,2010,2015,2020, 2024))+
  labs(x="", y="Category",
       title="Satisfaction with Democracy", color="Race",
       subtitle="On the whole, are you satisfied with the way
democracy works in the United States? N=21,406 (ANES)
Means and 95% Confidence Intervals") +
  scale_y_continuous(limits=c(2,5),labels = c("Not at all satisfied",
                                              "Not very satisfied",
                                              "Fairly satisfied",
                                              "Very Satisfied"))
b

eff <- anes3 %>%
  subset((extEfcacy_official_dont_care >=0 & extEfcacy_have_no_say>=0) |
           (is.na(extEfcacy_official_dont_care) & is.na(extEfcacy_have_no_say)))


eff$extEfcacy <- ifelse(eff$year==2024, ((eff$extEfcacy_official_dont_care + eff$extEfcacy_have_no_say)/2)*10, eff$extEfcacy)

eff <- eff %>%
  select(year, weight_full, extEfcacy, race) %>%
  subset(race==1 | race==2) %>%
  drop_na() %>%
  group_by(year, race) %>%
  summarise(value = weighted.mean(extEfcacy, weight_full),
            sd = sd(extEfcacy*weight_full),
            se = sd/sqrt(n()))

c<-ggplot(eff, aes(x=year, y=value, color=as.character(race))) +
  geom_line(linetype="dashed") +
  geom_point() +
  #  geom_line(linetype="dashed") +
  geom_errorbar(aes(ymin = (value -1.96*se), 
                    ymax = (value + 1.96*se)), width = 0.4) +
  theme_bw() +
  scale_color_manual(values=c("grey", "grey3"), labels=c("White", "Black"))+
  scale_y_continuous(limits=c(0,100)) +
  labs(x="", y="Mean",
       title="External Efficacy", color="Race",
       subtitle="Based on two items: People like me don't have any say
about what the government does, Agree (0), Neither (50), Disagree (100) /
I don't think public officials care much what people like me think, 
Agree (0), Neither (50), Disagree (100). N = 59,492 (ANES). 
Means and 95% Confidence Intervals.")
c

png(file = "PNAS/panel2-by-race.png",
    height = 6,
    width = 18,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(b,a,c,
                nrow = 1, ncol = 3, common.legend = T, legend = "bottom"))  

dev.off()

